package com.example.newsproject;
/*
 * @Description:
 * @Author: 山志凯
 * @Date: 2021-06-29
 * @Time: 15:18
 */

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

//viewpager里面放置的是view时，继承PagerAdapter，然后重写4个方法
public class HeaderPagerAdapter extends PagerAdapter {
    Context context;
    List<View>viewList;

    public HeaderPagerAdapter(Context context, List<View> viewList) {
        this.context = context;
        this.viewList = viewList;
    }

    @Override
    public int getCount() {
        return viewList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = viewList.get(position);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        View view = viewList.get(position);
        container.removeView(view);
    }
}
